<template>
    <div class="app-container">
        <el-card class="box-card" shadow="always" v-show="showSearch">
            <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
                <el-form-item label="模板名称" prop="templateName">
                    <el-input v-model="queryParams.templateName" placeholder="请输入模板名称" clearable
                        @keyup.enter="handleQuery" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleQuery">搜索</el-button>
                    <el-button @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>

        <el-card class="box-card" shadow="always" :body-style="{ padding: '20px' }">
            <!-- <div slot="header">
            <span></span>
        </div> -->
            <!-- card body -->
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" @click="handleAdd({ templateType: active })">添加模板
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="danger" :disabled="!selectedRows.length" @click="handleBatchDelete">删除
                    </el-button>
                </el-col>
                <right-toolbar :v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-tabs v-model="active" class="demo-tabs" @tab-click="handleClick">
                <el-tab-pane v-for="(item, index) in print_template_type" :key="index" :label="item.label"
                    :name="item.value"></el-tab-pane>
            </el-tabs>
            <el-table v-if="refreshTable" row-key="id" :default-expand-all="isExpandAll"
                :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" v-loading="loading"
                :data="dataSource" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center" :selectable="selectHandle" />
                <el-table-column label="模板名称" align="left" prop="templateName" />

                <el-table-column label="模板类型" align="left" prop="templateType">
                    <template #default="scope">
                        {{ selectDictLabel(print_template_type, scope.row.templateType) }}
                    </template>
                </el-table-column>
                <el-table-column label="备注" align="left" prop="remark" width="150" />
                <el-table-column label="是否默认" align="left" prop="defaultStatus">
                    <template #default="scope">
                        <el-switch active-value="1" inactive-value="0" @change="handleChangeStatus($event, scope.row)"
                            v-model="scope.row.defaultStatus" />
                    </template>
                </el-table-column>
                <el-table-column label="状态" align="left" prop="templateStatus">
                    <template #default="scope">
                        <el-tag :type="scope.row.templateStatus == 1 ? 'success' : 'danger'">
                            {{ scope.row.templateStatus == 1 ? '正常' : '禁用' }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template #default="scope">
                        <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
                        <el-divider direction="vertical"></el-divider>
                        <el-button type="text" @click="handleDesign(scope.row)">设计</el-button>
                        <el-divider direction="vertical"></el-divider>
                        <el-button type="text" @click="handleDelete(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- <pagination v-show="total > 0" :total="total" v-model:page.sync="queryParams.pageNum"
                v-model:limit.sync="queryParams.pageSize" @pagination="getList" /> -->
        </el-card>
        <addForm ref="modelForm" :categoryList="categoryList" @ok="getList"></addForm>
    </div>
</template>

<script>
import addForm from './modules/addForm.vue';
import listMixin from '@/mixins/listMixin';
import { changeStatus } from '@/api/goods/express';
export default {
    name: "templateList",
    dicts: ["inventory_status", "counting_method"],
    components: {
        addForm
    },
    mixins: [listMixin],
    data() {
        return {
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                templateType: '1'
            },
            urls: {
                list: '/order/template/list',
                delete: '/order/template',
            },
            isExpandAll: true,
            refreshTable: true,
            print_template_type: [],
            active: '1',
        }
    },
    mounted() {
        this.getDicts(['print_template_type']);
    },
    methods: {


        /**
         * @批量删除
         */
        handleBatchDelete() {
            if (this.selectedRows.length == 0) {
                this.$modal.msgError('请选择要删除的数据');
                return;
            }
            if (this.selectedRows.some(item => item.children && item.children.length > 0)) {
                this.$modal.msgError('请先删除子分类');
                return;
            }
            var ids = this.selectedRows.map(item => item.id).join(',');
            this.handleDelete(ids);
        },
        handleClick(tab, event) {
            console.log('event :>> ', event);
            console.log('tab :>> ', tab);
            this.queryParams.templateType = tab.props.name;
            console.log('this.queryParams.templateType :>> ', this.queryParams.templateType);
            this.getList();
        },
        handleChangeStatus(event, row) {
            changeStatus(row).then(res => {
                this.$modal.msgSuccess('操作成功');
                this.getList();
            })
        },
        handleDesign(row) {
            window.open('/order/express/template?id=' + row.id, '_blank');
        },

        /**
         * @重置查询条件
         */
        resetQuery() {
            this.queryParams.templateName = '';
            this.getList()
        },
    },
};
</script>
<style type="text/css" scoped>
.box-card {
    margin-top: 10px;
}
</style>